﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Ceremony.API.Application.Queries;
using Ceremony.API.Model;
using Ceremony.API.ParameterModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace Ceremony.API.Controllers
{
    [Route("api/v1/ceremony")]
    [ApiController]
    public class CommentController : ControllerBase
    {
        private readonly ILogger<CommentController> _logger;
        private readonly CeremonyContext _context;
        private readonly ICeremonyQueries _queries;
        public CommentController(ILogger<CommentController> logger, CeremonyContext context, ICeremonyQueries queries)
        {
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));
            _context = context ?? throw new ArgumentNullException(nameof(context));
            _queries = queries ?? throw new ArgumentNullException(nameof(queries));
        }
        [Route("comment")]
        [HttpPost]
        [ProducesResponseType((int)HttpStatusCode.OK)]
        [ProducesResponseType((int)HttpStatusCode.BadRequest)]
        public async Task<IActionResult> CreateComment([FromBody]CreateComment comment)
        {
            var commentModel = new Comment(comment.WechatMomentId, comment.CommentType, comment.FromUserId, comment.ToUserId, comment.CommentMessage);
            _context.Comments.Add(commentModel);
            var result = await _context.SaveChangesAsync();
            string resultStr = result > 0 ? "succeed" : "failed";
            _logger.LogInformation("----- Create Comment {Result}: {Comment} - {IdProperty}: {CommentId} ({@Comment})",
                resultStr,
                commentModel.GetType().Name,
                nameof(commentModel.Id),
                commentModel.Id,
                commentModel);
            if (result > 0)
            {
                return Ok();
            }
            else
            {
                return StatusCode(500, "创建评论失败");
            }
        }
    }
}